﻿using System;

namespace CS_Discovery
{
/// <summary>
/// 树状列表节点列表
/// </summary>
[System.Serializable()]
public class SkyTreeNodeList : System.Collections.CollectionBase
{
    /// <summary>
    /// 初始化对象
    /// </summary>
    public SkyTreeNodeList()
    {
    }
    /// <summary>
    /// 初始化对象
    /// </summary>
    /// <param name="node">列表所属节点对象</param>
    public SkyTreeNodeList(SkyTreeNode node)
    {
        myOwnerNode = node;
    }

    private SkyTreeNode myOwnerNode = null;
    /// <summary>
    /// 拥有该列表的节点对象
    /// </summary>
    [System.ComponentModel.Browsable(false)]
    public SkyTreeNode OwnerNode
    {
        get
        {
            return myOwnerNode; 
        }
    }
    
    /// <summary>
    /// 返回指定序号处的节点对象
    /// </summary>
    public SkyTreeNode this[int index]
    {
        get
        {
            return (SkyTreeNode)this.List[index]; 
        }
    }

    /// <summary>
    /// 向列表添加节点
    /// </summary>
    /// <param name="node">节点对象</param>
    /// <returns>新节点在列表中的序号</returns>
    public int Add(SkyTreeNode node)
    {
        if (node == null)
            throw new ArgumentNullException("node");
        if (myOwnerNode != null)
            node.Parent = myOwnerNode;
        return this.List.Add(node);
    }

    /// <summary>
    /// 向列表添加若干个节点
    /// </summary>
    /// <param name="nodes">节点列表,该列表中的元素类型必须为SkyTreeNode类型</param>
    public void AddRange(System.Collections.ICollection nodes)
    {
        if (nodes == null)
            throw new ArgumentNullException("nodes");
        foreach (SkyTreeNode node in nodes)
        {
            if (myOwnerNode != null)
                node.Parent = myOwnerNode;
            this.List.Add(node);
        }
    }

    /// <summary>
    /// 删除节点
    /// </summary>
    /// <param name="node">节点对象</param>
    public void Remove(SkyTreeNode node)
    {
        this.List.Remove(node);
    }

    /// <summary>
    /// 判断节点在列表中的序号
    /// </summary>
    /// <param name="node">节点对象</param>
    /// <returns>节点在列表中的从0开始的序号，若不存在则返回-1</returns>
    public int IndexOf(SkyTreeNode node)
    {
        return this.List.IndexOf(node);
    }

    /// <summary>
    /// 判断列表是否存在指定的节点对象
    /// </summary>
    /// <param name="node">节点对象</param>
    /// <returns>是否存在该节点</returns>
    public bool Contains(SkyTreeNode node)
    {
        return this.List.Contains(node);
    }
}//public class SkyTreeNodeList : System.Collections.CollectionBase
}
